//
//  HMMainTabBar.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/8.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

class HMMainTabBar: UITabBar {

    //定义一个闭包属性
    var composeClousure: (() -> ())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //定义按钮个数
        let buttonCount = 5
        
        let width = self.frame.size.width / CGFloat(buttonCount)
        
        let newFrame = CGRect(x: 0, y: 0, width: width, height: frame.size.height)
        
        var index = 0
        
        for subview in subviews{
        
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subview.frame = CGRectOffset(newFrame, CGFloat(index)*width, 0)
                
                index += index == 1 ? 2 : 1
                
                
            }
            
        self.composeButton.frame = CGRectMake(width*2, 0, width, frame.size.height)
        }
        //计算
        
        
    }
   //点击方法
   @objc private func didClickComposeButton(){
    
    composeClousure?()
    }
    
    
    //懒加载加好按钮
    private lazy var composeButton: UIButton = {
   
    let button = UIButton()
        
    button.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), forState: .Normal)
        
    button.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
    
    button.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: .Normal)
    button.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        //添加点击事件
        button.addTarget(self, action: Selector("didClickComposeButton"), forControlEvents: .TouchUpInside)
        
        self.addSubview(button)
        return button
   
}()

}

















